<!doctype html>
<html lang="zh-CN" th:replace="admin-layout :: layout(~{::title}, ~{}, ~{::script}, ~{::body}, 'paper-list')"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>试卷列表</title>
    <script>
        var $table = $("#table");
        var $remove = $("#removeBtn");
        var $update = $("#updateBtn");
        var $info = $("#infoBtn");
        var $question = $("#questionBtn");
        var $user = $("#userBtn");
        var $preview = $("#previewBtn");
        var $copy = $("#copyBtn");
        var $search = $("#searchBtn");
        var selections = []

        //$(this).serialize()
        $(function () {
            initTable($table, [{
                checkbox: true
            }, {
                field: 'name',
                title: '试卷名称',
            }, {
                field: 'time',
                title: '答卷时间（分钟）',
            }, {
                field: 'score',
                title: '试卷分数',
            }, {
                field: 'qstNum',
                title: '试题数量',
            }, {
                field: 'type',
                title: '试卷类型',
                formatter: typeFormatter
            }], function () {
                $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
                $update.prop('disabled', !$table.bootstrapTable('getSelections').length);
                $info.prop('disabled', !$table.bootstrapTable('getSelections').length);
                $question.prop('disabled', !$table.bootstrapTable('getSelections').length);
                $user.prop('disabled', !$table.bootstrapTable('getSelections').length);
                $preview.prop('disabled', !$table.bootstrapTable('getSelections').length);
                $copy.prop('disabled', !$table.bootstrapTable('getSelections').length);

                selections = getIdSelections()
            })
        })

        function typeFormatter(value, row, index) {
            if (row.type === 0) {
                return "随机出题";
            } else if (row.type === 1) {
                return "固定出题";
            }
        }

        $remove.click(function () {
            confirmAlert(function () {
                ajaxJson("/admin/paper/ajax/delete", {"ids": selections.toString()}, function (res) {
                    if (res.status) {
                        refreshTable();
                    }
                })
            })
        })

        $update.click(function () {
            location.href = "/admin/paper/save?id=" + selections[0];
        })

        $info.click(function () {
            var ids = getIdSelections()
            if (ids) {
                location.href = "/admin/paper/info/" + ids[0]
            }
        })

        $question.click(function () {
            location.href = "/admin/paper/question/" + selections[0];
        })

        $user.click(function () {
            var ids = getIdSelections()
            if (ids) {
                location.href = "/admin/paper/user/" + ids[0]
            }
        })

        function getIdSelections() {
            return $.map($table.bootstrapTable('getSelections'), function (row) {
                return row.id
            })
        }

        $search.click(function () {
            refreshTable();
        })

        function refreshTable(){
            $table.bootstrapTable('refresh');
        }
    </script>
</head>
<body>
<!--<ol class="breadcrumb">
    <li class="active">试卷列表</li>
</ol>-->
<div class="">
    <form id="toolbar">
        <div class="row">
            <div class="col-sm-4 col-md-4 col-lg-3">
                <input type="text" class="form-control fyl-clear" name="name"
                       placeholder="试卷名称">
            </div>
            <div class="col-sm-4 col-md-4 col-lg-3">
                <a href="javascript:void(0)" class="btn btn-primary" id="searchBtn">查询</a>
                <a href="javascript:void(0)" class="btn btn-warning" id="clearSearchBtn">清空</a>
            </div>
        </div>
    </form>
</div>
<hr>
<div class="fyl-mb20">
    <a th:href="@{/admin/paper/save}" class="btn btn-primary">添加</a>
<!--    <button type="button" class="btn btn-primary" id="previewBtn" disabled>预览</button>-->
    <button type="button" class="btn btn-primary" id="questionBtn" disabled>试题</button>
    <button type="button" class="btn btn-primary" id="updateBtn" disabled>修改</button>
    <button type="button" class="btn btn-danger" id="removeBtn" disabled>删除</button>
</div>
<table id="table" data-url="/admin/paper/ajax/list"></table>
</body>
</html>